package com.banmao.mall.service.admin.impl;

import com.banmao.mall.mapper.*;
import com.banmao.mall.model.bo.admin.user.*;
import com.banmao.mall.model.vo.admin.user.*;
import com.banmao.mall.service.admin.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    // 注入Mapper组件

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FoorprintMapper foorprintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public UserListVO userList(UserListBO userListBO) {

        PageHelper.startPage(userListBO.getPage(), userListBO.getLimit());

        List<UserVO> userVOS = userMapper.selectUserByCondition(userListBO);

        PageInfo<UserVO> userVOPageInfo = new PageInfo<>(userVOS);
        long totalCount = userVOPageInfo.getTotal();

        UserListVO userListVO = new UserListVO();
        userListVO.setTotal(totalCount);
        userListVO.setItems(userVOS);

        return userListVO;
    }

    @Override
    public AddressListVO addressList(AddressListBO addressListBO) {

        PageHelper.startPage(addressListBO.getPage(), addressListBO.getLimit());

        List<AddressVO> addressVOS = addressMapper.selectAddressByCondition(addressListBO);

        PageInfo<AddressVO> addressVOPageInfo = new PageInfo<>(addressVOS);
        long totalCount = addressVOPageInfo.getTotal();

        // 遍历集合 获取province、city、area
        for (AddressVO addressVO : addressVOS) {
            // 从region表中获取province
            String province = regionMapper.selectNameById(addressVO.getProvinceId());
            addressVO.setProvince(province);
            String city = regionMapper.selectNameById(addressVO.getCityId());
            addressVO.setCity(city);
            String area = regionMapper.selectNameById(addressVO.getAreaId());
            addressVO.setArea(area);
        }

        // 创建AddressListVO对象
        AddressListVO addressListVO = new AddressListVO();
        addressListVO.setTotal(totalCount);
        addressListVO.setItems(addressVOS);

        return addressListVO;
    }

    @Override
    public CollectListVO collectList(CollectListBO collectListBO) {

        // 获取第x页，y条内容
        PageHelper.startPage(collectListBO.getPage(), collectListBO.getLimit());

        List<CollectVO> collectVOS = collectMapper.selectCollectByCondition(collectListBO);

        // 用PageInfo封装获得分页信息
        PageInfo<CollectVO> collectVOPageInfo = new PageInfo<CollectVO>(collectVOS);
        long totalCount = collectVOPageInfo.getTotal();

        CollectListVO collectListVO = new CollectListVO();
        collectListVO.setTotal(totalCount);
        collectListVO.setItems(collectVOS);

        return collectListVO;

    }

    @Override
    public FootprintListVO footprintList(FootprintListBO footprintListBO) {

        PageHelper.startPage(footprintListBO.getPage(), footprintListBO.getLimit());

        List<FootprintVO> footprintVOS = foorprintMapper.selectFootprintByCondition(footprintListBO);

        PageInfo<FootprintVO> footprintVOPageInfo = new PageInfo<>(footprintVOS);
        long totalCount = footprintVOPageInfo.getTotal();

        FootprintListVO footprintListVO = new FootprintListVO();
        footprintListVO.setTotal(totalCount);
        footprintListVO.setItems(footprintVOS);

        return footprintListVO;
    }

    @Override
    public HistoryListVO historyList(HistoryListBO historyListBO) {

        PageHelper.startPage(historyListBO.getPage(), historyListBO.getLimit());

        List<HistoryVO> historyVOS = searchHistoryMapper.selectHistoryByCondition(historyListBO);

        PageInfo<HistoryVO> historyVOPageInfo = new PageInfo<>(historyVOS);
        long totalCount = historyVOPageInfo.getTotal();

        HistoryListVO historyListVO = new HistoryListVO();
        historyListVO.setTotal(totalCount);
        historyListVO.setItems(historyVOS);

        return historyListVO;
    }

    @Override
    public FeedbackListVO feedbackList(FeedbackListBO feedbackListBO) {

        PageHelper.startPage(feedbackListBO.getPage(), feedbackListBO.getLimit());

        List<FeedbackVO> feedbackVOS = feedbackMapper.selectFeedbackByCondition(feedbackListBO);

        PageInfo<FeedbackVO> feedbackVOPageInfo = new PageInfo<>(feedbackVOS);
        long totalCount = feedbackVOPageInfo.getTotal();

        FeedbackListVO feedbackListVO = new FeedbackListVO();
        feedbackListVO.setTotal(totalCount);
        feedbackListVO.setItems(feedbackVOS);

        return feedbackListVO;
    }

}
